<template>
  <div class="product-list">
    <div class="page-header">
      <h2>商品列表</h2>
      <div class="search-bar">
        <input
          v-model="searchKeyword"
          type="text"
          placeholder="搜索商品..."
          class="search-input"
        />
      </div>
    </div>
    
    <div v-if="loading" class="loading">加载中...</div>
    <div v-else-if="error" class="error">{{ error }}</div>
    <div v-else-if="filteredProducts.length === 0" class="empty">
      暂无商品
    </div>
    
    <div v-else class="products-grid">
      <div v-for="product in filteredProducts" :key="product.id" class="product-card">
        <img :src="product.image" :alt="product.name" class="product-image" />
        <div class="product-info">
          <h3>{{ product.name }}</h3>
          <p class="product-description">{{ product.description }}</p>
          <div class="product-meta">
            <span class="price">¥{{ product.price }}</span>
            <span class="stock">库存: {{ product.stock }}</span>
            <span class="category">{{ product.category }}</span>
          </div>
          <div class="product-actions">
            <div class="quantity-control">
              <button 
                @click="decreaseQuantity(product.id)" 
                class="quantity-btn"
                :disabled="getQuantity(product.id) <= 1"
              >
                -
              </button>
              <span class="quantity">{{ getQuantity(product.id) }}</span>
              <button 
                @click="increaseQuantity(product.id)" 
                class="quantity-btn"
                :disabled="getQuantity(product.id) >= product.stock"
              >
                +
              </button>
            </div>
            <button 
              @click="addToCart(product)" 
              class="add-to-cart-btn"
              :disabled="product.stock === 0"
            >
              {{ product.stock === 0 ? '缺货' : '加入购物车' }}
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { storeToRefs } from 'pinia'
import { useProductStore } from '../../stores/product'
import { useCartStore } from '../../stores/cart'

const productStore = useProductStore()
const cartStore = useCartStore()

const { products, loading, error } = storeToRefs(productStore)
const searchKeyword = ref('')

// 商品数量管理
const productQuantities = ref({})

const filteredProducts = computed(() => {
  if (!searchKeyword.value) return products.value
  return productStore.searchProducts(searchKeyword.value)
})

onMounted(() => {
  productStore.fetchProducts()
})

const getQuantity = (productId) => {
  return productQuantities.value[productId] || 1
}

const increaseQuantity = (productId) => {
  const product = products.value.find(p => p.id === productId)
  if (product && getQuantity(productId) < product.stock) {
    productQuantities.value[productId] = getQuantity(productId) + 1
  }
}

const decreaseQuantity = (productId) => {
  if (getQuantity(productId) > 1) {
    productQuantities.value[productId] = getQuantity(productId) - 1
  }
}

const addToCart = async (product) => {
  const quantity = getQuantity(product.id)
  try {
    await cartStore.addToCart(product, quantity)
    // 重置该商品数量
    productQuantities.value[product.id] = 1
    alert('商品已添加到购物车！')
  } catch (err) {
    alert('添加商品失败：' + err.message)
  }
}
</script>

<style scoped>
.product-list {
  background: white;
  border-radius: 8px;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.page-header h2 {
  margin: 0;
  color: #333;
}

.search-bar {
  flex: 1;
  max-width: 300px;
  margin-left: 20px;
}

.search-input {
  width: 100%;
  padding: 10px 15px;
  border: 1px solid #ddd;
  border-radius: 25px;
  font-size: 14px;
  transition: border-color 0.3s;
}

.search-input:focus {
  outline: none;
  border-color: #667eea;
}

.loading, .error, .empty {
  text-align: center;
  padding: 40px;
  font-size: 16px;
}

.error {
  color: #e74c3c;
}

.empty {
  color: #666;
}

.products-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

.product-card {
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  overflow: hidden;
  transition: transform 0.2s, box-shadow 0.2s;
}

.product-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.product-image {
  width: 100%;
  height: 180px;
  object-fit: cover;
}

.product-info {
  padding: 15px;
}

.product-info h3 {
  margin: 0 0 8px 0;
  color: #333;
  font-size: 16px;
}

.product-description {
  color: #666;
  font-size: 13px;
  margin-bottom: 12px;
  line-height: 1.4;
  height: 36px;
  overflow: hidden;
}

.product-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 15px;
}

.price {
  color: #ff4757;
  font-size: 18px;
  font-weight: bold;
}

.stock {
  color: #666;
  font-size: 12px;
}

.category {
  background: #f0f2ff;
  color: #667eea;
  padding: 3px 6px;
  border-radius: 3px;
  font-size: 11px;
}

.product-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.quantity-control {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 15px;
}

.quantity-btn {
  width: 30px;
  height: 30px;
  border: 1px solid #ddd;
  background: white;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  font-weight: bold;
  transition: all 0.2s;
}

.quantity-btn:hover:not(:disabled) {
  background: #f8f9ff;
  border-color: #667eea;
  color: #667eea;
}

.quantity-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.quantity {
  font-weight: bold;
  color: #333;
  min-width: 30px;
  text-align: center;
}

.add-to-cart-btn {
  padding: 10px;
  background: #667eea;
  color: white;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  transition: background-color 0.3s;
}

.add-to-cart-btn:hover:not(:disabled) {
  background: #5a6fd8;
}

.add-to-cart-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}
</style>